<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="${ctx}/resources/layui/css/layui.css">
<link rel="stylesheet" href="${ctx}/resources/layui_ext/dtree/dtree.css">
<link rel="stylesheet" href="${ctx}/resources/layui_ext/dtree/font/dtreefont.css">
</head>
<body>
	<!-- 搜索条件开始 -->
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
	  <legend>查询条件</legend>
	</fieldset>
	<form action="" class="layui-form" method="post" id="searchFrm" >
		<div class="layui-form-item">
		    <div class="layui-inline">
		      <label class="layui-form-label">角色名称:</label>
		      <div class="layui-input-inline">
		        <input type="text" name="rolename"  autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-inline">
		      <label class="layui-form-label">角色备注:</label>
		      <div class="layui-input-inline">
		        <input type="text" name="roledesc"  autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-inline">
		      <label class="layui-form-label">是否可用:</label>
		      <div class="layui-input-inline">
		       		 <input type="radio" name="available" value="1" title="可用">
					 <input type="radio" name="available" value="0" title="不可">
		      </div>
		    </div>
		    <div class="layui-inline">
		    	<button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-search" id="doSearch">查询</button>
		      	<button type="reset" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh">重置</button>
		    </div>
		 </div>
	</form>
	
	<!-- 搜索条件结束 -->
	
	
	<!-- 数据表格开始 -->
	<table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>
	<div style="display: none;" id="roleToolBar">
		    <button type="button" class="layui-btn layui-btn-sm" lay-event="add">增加</button>
		    <button type="button" class="layui-btn layui-btn-sm" lay-event="batchDelete">批量删除</button>
	</div>
	<div  id="roleBar" style="display: none;">
	  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
	  <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="selectRoleMenu">分配菜单</a>
	  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</div>
	<!-- 数据表格结束 -->
	
	<!-- 添加和修改的弹出层开始 -->
	<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
		<form class="layui-form" id="dataFrm" lay-filter="dataFrm">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">角色名称:</label>
					<div class="layui-input-block">
						<input type="hidden" name="roleid">
						<input type="text" name="rolename"  placeholder="请输入角色名称" autocomplete="off" class="layui-input">
					</div>
				</div>
			</div>
			
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">角色备注:</label>
					<div class="layui-input-block">
						<input type="text" name="roledesc" placeholder="请输入角色备注" autocomplete="off" class="layui-input">
					</div>
				</div>
			</div>
			
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">是否可用:</label>
					<div class="layui-input-inline">
						 <input type="radio" name="available" value="1" checked="checked" title="可用">
						 <input type="radio" name="available" value="0" title="不可用">
					</div>
				</div>
			</div>
			
		</form>
	</div>
	<!-- 添加和修改的弹出层结束 -->
	
	<div style="display: none;" id="selectRoleMenu">
		<ul id="menuTree" class="dtree" data-id="0"></ul>
	</div>
	
	<script src="${ctx}/resources/layui/layui.js"></script>
	<script type="text/javascript">
		var tableIns
		  layui.extend({
			    dtree: '${ctx}/resources/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
			  }).use([ 'jquery', 'layer', 'form', 'table','laydate','dtree' ], function() {
			var $ = layui.jquery;
			var layer = layui.layer;
			var form = layui.form;
			var table = layui.table;
			var laydate=layui.laydate;
			var dtree = layui.dtree;
			
			//绑定时间选择器
			laydate.render({
				elem:'#startTime'
			});
			laydate.render({
				elem:'#endTime'
			})
			
			
			//渲染数据表格
			tableIns=table.render({
				 elem: '#roleTable'   //渲染的目标对象
			    ,url:'${ctx}/role/loadAllRole.action' //数据接口
			    ,title: '用户数据表'//数据导出来的标题
			    ,toolbar:"#roleToolBar"   //表格的工具条
			    //,height:'full-300'
			    ,cellMinWidth:100 //设置列的最小默认宽度
			    ,page: true  //是否启用分页
			    ,cols: [[   //列表数据
				     {type: 'checkbox', fixed: 'left'}
				      ,{field:'roleid', title:'ID',align:'center',width:'80'}
				      ,{field:'rolename', title:'角色名称',align:'center',width:'150'}
				      ,{field:'roledesc', title:'角色简介',align:'center',width:'200'}
				      ,{field:'available', title:'是否可用',align:'center',width:'100',templet:function(d){
				    	  return d.available=='1'?'<font color=blue>可用</font>':'<font color=red>不可用</font>';
				      }}
				      ,{fixed: 'right', title:'操作', toolbar: '#roleBar', width:'250',align:'center'}
				    ]]
				,done:function(data,curr,count){
			    	//不是第一页时如果当前返回的的数据为0那么就返回上一页
			    	console.log(data)
			    	if(data.data.length==0&&curr!=1){
			    		tableIns.reload({
						    page:{ curr:curr-1 }
						});
			    	}
			    }
			})
			$("#doSearch").click(function (){
				var parms = $("#searchFrm").serialize();
				tableIns.reload({
					url:'${ctx}/role/loadAllRole.action?'+parms
					,page:{curr:1}
				})
			})

			//监听行工具事件
		   table.on('tool(roleTable)', function(obj){
			   var data = obj.data; //获得当前行数据
			   var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
			  if(layEvent === 'del'){ //删除
				 deleterole(data);
			     
			   } else if(layEvent === 'edit'){ //编辑
			     //do something
				   layer.msg("修改")
				   openUpdaterole(obj.data);
			   } else if(layEvent === 'selectRoleMenu'){
				 openselectRoleMenu(data);
				 
			   }
			 });
			
			
			
			//监听头部工具栏事件
			table.on("toolbar(roleTable)",function(obj){
				 switch(obj.event){
				    case 'add':
			      		openAddrole();
				    break;
				    case 'batchDelete':
			    		deleteBatch();
				    break;
				  };
			})
			
			function deleterole(data){
				layer.confirm('真的删除【'+data.rolename+'】这个角色吗', function(index){
					$.post("${ctx}/role/deleteRole.action",{roleid:data.roleid},function(res){
						layer.close(index);
						tableIns.reload();
					})
				});
			}
			
			function deleteBatch(){
				var checkStatus = table.checkStatus('roleTable');
				var data = checkStatus.data;
				var params="";
				$.each(data,function(i,item){
			    	if(i==0){
			    		params+="ids="+item.roleid;
			    	}else{
			    		params+="&ids="+item.roleid;
			    	}
			    });
			    layer.confirm('真的删除选中的这些角色吗', function(index){
				   console.log(params)
			       $.post("${ctx}/role/deleteBatchRole.action",params,function(res){
			    	   layer.msg(res.msg);
			    	   tableIns.reload();
			       })
			     }); 
			}
			
			var mainIndex;
			//打开添加页面
			function openAddrole(){
				mainIndex = layer.open({
					type:1,
					title:'添加角色',
					content:$("#saveOrUpdateDiv"),
					area:['360px'],
					btnAlign:'c',
					btn:['<div class="layui-icon layui-icon-release">保存</div>','<div class="layui-icon layui-icon-close">关闭</div>'],
					success:function(index){
						$("#dataFrm")[0].reset();
					},
					yes:function(index, layero){
						layer.msg("保存");
						var params = $("#dataFrm").serialize();
						console.log(params);
						$.post("${ctx}/role/addRole.action",params,function(res){
							layer.msg(res.msg);
							layer.close(mainIndex);
							tableIns.reload();
						})
					},
					btn2:function(index, layero){
						layer.msg("关闭");
						return true;
					}
				});
			}
			
			function openUpdaterole(data){
				mainIndex = layer.open({
					type:1,
					title:'修改角色',
					content:$("#saveOrUpdateDiv"),
					area:['360px'],
					btnAlign:'c',
					btn:['<div class="layui-icon layui-icon-release">保存</div>','<div class="layui-icon layui-icon-close">关闭</div>'],
					success:function(index){
						$("#dataFrm")[0].reset();
						form.val("dataFrm",data);
					},
					yes:function(index, layero){
						layer.msg("保存");
						var params = $("#dataFrm").serialize();
						console.log(params);
						$.post("${ctx}/role/updateRole.action",params,function(res){
							layer.msg(res.msg);
							layer.close(mainIndex)
							tableIns.reload();
						})
					},
					btn2:function(index, layero){
						layer.msg("关闭");
						return true;
					}
				});
			}
			
			function openselectRoleMenu(data){
				var menuTree;
				mainIndex = layer.open({
					type:1,
					title:'分配角色',
					content:$("#selectRoleMenu"),
					area:['400px','500px'],
					btnAlign:'c',
					btn:['<div class="layui-icon layui-icon-release">确认分配</div>','<div class="layui-icon layui-icon-close">取消分配</div>'],
					success:function(index){
						dtree.render({
					      elem: "#menuTree",
						  dataStyle: "layuiStyle",  //使用layui风格的数据格式
					      response:{message:"msg",statusCode:0},  //修改response中返回数据的定义
				          dataFormat: "list",  //配置data的风格为list
				          checkbar: true,  
			              checkbarType: "all", // 默认就是all，其他的值为： no-all  p-casc   self  only\
			              checkbarData: "choose",
			              url: "${ctx}/role/initRoleMenuTreeJson.action?roleid="+data.roleid
					    });
					},
					yes:function(){
						var nodes = dtree.getCheckbarNodesParam("menuTree");
						var roleid=data.roleid;
						var params="roleid="+roleid;
						
						$.each(nodes,function(i,item){
							params+="&ids="+item.nodeId;
						})
						$.post("${ctx}/role/saveRoleMenu.action",params,function(obj){
							layer.msg(obj.msg);
						})
						layer.close(mainIndex);
					}
				});
			}
			
			
			/*
			dtree.renderSelect({
			  elem: "#roleTree",
			  url: "${ctx}/role/loadroleManagerLeftTreeJson.action",
			  dataStyle: "layuiStyle",  //使用layui风格的数据格式
		      response:{message:"msg",statusCode:0},  //修改response中返回数据的定义
              dataFormat: "list",  //配置data的风格为list
              method: 'post'
			});
			$("body").on("click", function(event){
			  $("div[dtree-id][dtree-select]").removeClass("layui-form-selected");
			  $("div[dtree-id][dtree-card]").removeClass("dtree-select-show layui-anim layui-anim-upbit");
			});
			*/
			
		});
	</script>
</body>
</html>